const s = Symbol('break visit'),
  y = Symbol('skip children'),
  u = Symbol('remove item');
function l(e, t) {
  'type' in e &&
    e.type === 'document' &&
    (e = { start: e.start, value: e.value }),
    m(Object.freeze([]), e, t);
}
l.BREAK = s;
l.SKIP = y;
l.REMOVE = u;
l.itemAtPath = (e, t) => {
  let n = e;
  for (const [o, i] of t) {
    const r = n == null ? void 0 : n[o];
    if (r && 'items' in r) n = r.items[i];
    else return;
  }
  return n;
};
l.parentCollection = (e, t) => {
  const n = l.itemAtPath(e, t.slice(0, -1)),
    o = t[t.length - 1][0],
    i = n == null ? void 0 : n[o];
  if (i && 'items' in i) return i;
  throw new Error('Parent collection not found');
};
function m(e, t, n) {
  let o = n(t, e);
  if (typeof o == 'symbol') return o;
  for (const i of ['key', 'value']) {
    const r = t[i];
    if (r && 'items' in r) {
      for (let f = 0; f < r.items.length; ++f) {
        const c = m(Object.freeze(e.concat([[i, f]])), r.items[f], n);
        if (typeof c == 'number') f = c - 1;
        else {
          if (c === s) return s;
          c === u && (r.items.splice(f, 1), (f -= 1));
        }
      }
      typeof o == 'function' && i === 'key' && (o = o(t, e));
    }
  }
  return typeof o == 'function' ? o(t, e) : o;
}
export { l as visit };
